package model

import (
	"awesomeProject2/global"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type Teacher struct {
	gorm.Model
	Email             string `json:"email"  gorm:"type:varchar(255);"`
	Gender            *bool  `json:"gender"  gorm:"type:tinyint"`
	TeacherName       string `json:"teacherName" gorm:"type:varchar(30);"`
	BirthDay          string `json:"birthDay"  gorm:"type:varchar(30)"`
	Phone             string `json:"phone"  gorm:"type:varchar(255);"`
	TeacherType       string `json:"teacherType"  gorm:"type:varchar(255);"`
	PTitle            string `json:"pTitle"  gorm:"type:varchar(255);"`
	MajorName         string `json:"majorName"  gorm:"type:varchar(255);"`
	ResearchDirection string `json:"researchDirection"  gorm:"type:varchar(255);"`
	UserId            uint   `json:"userid" gorm:"type:bigint"`
	Avatar            string `json:"avatar"  gorm:"type:varchar(255);"`
	Resume            string `json:"resume"  gorm:"type:varchar(255);"`
}

func GetCurrentTeacher(c *gin.Context) (err error, teacher Teacher) {
	id, _ := c.Get("userid")
	return GetTeacherByDynamicQuery(&Teacher{UserId: id.(uint)})
}

func GetUserIdsByTeacherName(teachName string) (userIds []uint, err error) {
	var teachers []Teacher
	err = global.GVA_DB.Where("teacher_name LIKE ?", "%"+teachName+"%").Find(&teachers).Error
	for _, teacher := range teachers {
		userIds = append(userIds, teacher.UserId)
	}
	return userIds, err
}

func GetCurrentTeacherId(c *gin.Context) (uint, error) {
	err, teacher := GetCurrentTeacher(c)
	return teacher.ID, err
}

func GetTeacherByDynamicQuery(query *Teacher) (err error, teacher Teacher) {
	err = global.GVA_DB.Where(query).First(&teacher).Error
	return err, teacher
}

func GetTeacherById(id uint) (err error, teacher Teacher) {
	err = global.GVA_DB.First(&teacher, id).Error
	return err, teacher
}

func CreateTeacher(teacher Teacher) (err error) {
	err = global.GVA_DB.Create(&teacher).Error
	return err
}
func DeleteTeacher(id int) (err error) {
	err = global.GVA_DB.Where("id = ?", id).Delete(&Teacher{}).Error
	return err
}

func UpdateTeacher(teacher *Teacher) (err error) {
	err = global.GVA_DB.Find(&Teacher{}, teacher.ID).Updates(teacher).Error
	return err
}

func GetPaperToShow(userid uint) (err error, list interface{}, total int64) {
	var papers []Paper
	var tt int64
	err = global.GVA_DB.Where("user_id = ? AND is_visible = 1", userid).Find(&papers).Count(&tt).Error
	return err, papers, tt
}
func GetProjectToShow(userid uint) (err error, list interface{}, total int64) {
	var projects []Project
	var tt int64
	err = global.GVA_DB.Where("user_id = ? AND is_visible = 1", userid).Find(&projects).Count(&tt).Error

	return err, projects, tt
}

func GetWorkToShow(userid uint) (err error, list interface{}, total int64) {
	var works []Work
	var tt int64
	err = global.GVA_DB.Where("user_id = ? AND is_visible = 1", userid).Find(&works).Count(&tt).Error

	return err, works, tt
}
func GetPatentToShow(userid uint) (err error, list interface{}, total int64) {
	var patents []Patent
	var tt int64
	err = global.GVA_DB.Where("user_id = ? AND is_visible = 1", userid).Find(&patents).Count(&tt).Error

	return err, patents, tt
}
func GetAchievementToShow(userid uint) (err error, list interface{}, total int64) {
	var achievemts []Achievement
	var tt int64
	err = global.GVA_DB.Where("user_id = ? AND is_visible = 1", userid).Find(&achievemts).Count(&tt).Error

	return err, achievemts, tt
}
func GetAllTeachers() (error, interface{}) {
	var data []Teacher
	err := global.GVA_DB.Find(&data).Error
	return err, data
}
